package com.k.kc.enums;

import java.util.*;
import java.util.stream.Collectors;

public enum SystemTypeEnum {

    SYS_A("1", "SystemA", "系统A"),
    SYS_B("2", "SystemB", "系统B"),
    SYS_DEFAULT("9", "SystemDefault", "默认系统");

    private final String value;
    private final String code;
    private final String desc;

    public String getCode() {
        return this.code;
    }

    public String getValue() {
        return value;
    }

    SystemTypeEnum(String value, String code, String desc) {
        this.value = value;
        this.code = code;
        this.desc = desc;
    }

    /**
     * 是否有效的 value
     *
     * @param v value
     * @return boolean
     */
    public static boolean existValue(String v) {
        return Arrays.stream(SystemTypeEnum.values()).anyMatch(itm -> itm.getValue().equals(v));
    }

    /**
     * value -> desc 找不到返回全部
     *
     * @param v value
     * @return desc
     */
    public static String valueToDesc(String v) {
        return Arrays.stream(SystemTypeEnum.values()).filter(itm -> itm.value.equals(v)).findFirst().orElse(SystemTypeEnum.SYS_DEFAULT).desc;
    }

    /**
     * Usage ex:
     * {@code
     *     switch (SystemTypeEnum.matchOpValue(type)){
     *         case SYS_A:
     *             System.out.println();
     *             break;
     *         case SYS_B:
     *             System.out.println();
     *             break;
     *         default:
     *             throw new CommonException("Unknown type");
     *     }
     * }
     *
     * @param v value
     * @return SystemTypeEnum
     */
    public static SystemTypeEnum matchOpValue(String v) {
        return Arrays.stream(SystemTypeEnum.values()).filter(itm -> itm.value.equals(v)).findFirst().orElse(null);
    }

    public static Optional<SystemTypeEnum> matchOpValueOptional(String v) {
        return Arrays.stream(SystemTypeEnum.values()).filter(a->a.value.equals(v)).findFirst();
    }

    public static List<Map<String,String>> toSelectList(){
        return Arrays.stream(SystemTypeEnum.values()).map(a->{
            Map<String,String> map = new HashMap<>();
            map.put("value",a.value);
            map.put("text",a.desc);
            return map;
        }).collect(Collectors.toList());
    }
    public static Map<String,String> toSelectMap(){
        Map<String,String> map = new HashMap<>();
        Arrays.stream(SystemTypeEnum.values()).map(a-> map.put(a.value,a.desc));
        return map;
    }

    public static void main(String[] args) {
        List<Map<String,String>> list = SystemTypeEnum.toSelectList();
        System.out.println(list);
    }

}
